from PyQt5.QtWidgets import *
from PyQt5 import QtGui
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import pymysql
import PIL
from PIL.ImageQt import ImageQt,Image
import base64
import io
import pickle
import  sys

import interface.student.stu_main0 as stu_main0
import interface.Main0_Window as Main0_Window
import interface.student.stu_query_history_course_window as sqhcw
import database.admin.setting as setting
import interface.student.course_table_Window as course_table_Window
import interface.student.select_course_Window as select_course_Window
import interface.student.to_be_comment_Window as to_be_comment_Window
class stu_main0_window(QMainWindow,stu_main0.Ui_Form):###点击用户身份弹出的登陆小弹窗
    def __init__(self,id):
        QMainWindow.__init__(self)
        stu_main0.Ui_Form.__init__(self)
        self.setupUi(self)
        self.setWindowTitle("学生操作界面")
        self.id=id
        self.quit.clicked.connect(self.close)
        self.back.clicked.connect(self.load_pre)
        self.init()
        self.review_history.clicked.connect(self.load_Stu_History)
        self.select_course.clicked.connect(self.load_sc)
        ap_ = QPixmap("student/ap.png")
        self.ap.setPixmap(ap_)
        self.ap.setScaledContents(True)
        bp_ = QPixmap("student/bp.png")
        self.bp.setPixmap(bp_)
        self.bp.setScaledContents(True)
        cp_ = QPixmap("student/cp.png")
        self.cp.setPixmap(cp_)
        self.cp.setScaledContents(True)
        dp_ = QPixmap("student/dp.png")
        self.dp.setPixmap(dp_)
        self.dp.setScaledContents(True)
        self.select_table.clicked.connect(self.course_table_show)
        self.assess.clicked.connect(self.comment_course_show)
        p = QPalette()
        p_ = QPixmap("s000.jpg")
        p_ = p_.scaled(self.width(), self.height())
        p.setBrush(QPalette.Background, QBrush(p_))
        self.setPalette(p)
    def comment_course_show(self):
        self.comment_window=to_be_comment_Window.to_be_comment_window(self.id)
        self.comment_window.show()

    def course_table_show(self):
        #print("yes")
        self.course_table_window=course_table_Window.course_table_window(self.id)
        self.course_table_window.show()
    def load_sc(self):
        with open("admin/time.pkl", 'rb') as f:
            begin = pickle.load(f)
            end = pickle.load(f)
            term = pickle.load(f)
        self.begin = begin
        now = QDateTime.currentDateTime()
        print("yes", now)

        if (now > end):
            QMessageBox.warning(self, "提示", "选课时间已过")
        elif (now < begin):
            QMessageBox.warning(self, "提示", "未到选课时间")
        else:
            print("yes")
            self.sc=select_course_Window.select_course_window(self.id)
            self.sc.show()
            self.close()
    def load_pre(self):
        self.pre=Main0_Window.Main0_Window()
        self.close()
        self.pre.show()
    def load_Stu_History(self):
        self.stu_history=sqhcw.Stu_QHC(self.name,self.id,self.grade)
        self.stu_history.show()
    def pil2pixmap(self, im):
        if im.mode == "RGB":
            r, g, b = im.split()
            im = Image.merge("RGB", (b, g, r))
        elif im.mode == "RGBA":
            r, g, b, a = im.split()
            im = Image.merge("RGBA", (b, g, r, a))
        elif im.mode == "L":
            im = im.convert("RGBA")
        # Bild in RGBA konvertieren, falls nicht bereits passiert
        im2 = im.convert("RGBA")
        data = im2.tobytes("raw", "RGBA")
        qim = QtGui.QImage(data, im.size[0], im.size[1], QtGui.QImage.Format_ARGB32)
        pixmap = QtGui.QPixmap.fromImage(qim)
        return pixmap
    def init(self):
        self.connect = pymysql.connect(**setting.config)
        self.cursor=self.connect.cursor()
        sql_="select * from students where id=%s"
        self.cursor.execute(sql_,self.id)
        data=self.cursor.fetchone()
        id_=self.id
        name_=data[1]
        sex_=data[2]
        grade_=data[3]
        school_=data[4]
        maj=data[6]
        self.name=name_
        self.grade=grade_
        #print(data)
        if (data[5]!= None):
            data1 = base64.b64decode(data[5])
            file_like = io.BytesIO(data1)
            img = PIL.Image.open(file_like)
        #img.show()
        #map_ = QtGui.QPixmap(img)
        #img= PIL.ImageQt.ImageQt(map_)
        #qim = ImageQt(img)
        #map_ = QtGui.QPixmap.fromImage(qim)
            map_=self.pil2pixmap(img)
            self.picture.setPixmap(map_)
            self.picture.setScaledContents(True)
        else:
            self.picture.setText("暂无照片")
        self.id_label.setText(id_)
        self.name_label.setText(name_)
        self.grade_label.setText(grade_)
        self.school_label.setText(school_)
        self.sex_label.setText(sex_)
        self.major.setText(maj)